package mh.demo.springboot.system;

import org.springframework.boot.env.PropertiesPropertySourceLoader;
import org.springframework.boot.env.PropertySourceLoader;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.util.List;

// 资源properties或yml加载
public class PropertySourceLoaderDemo {

    public void run() {
        try {
            // 加载properties文件
            PropertySourceLoader propertyLoader = new PropertiesPropertySourceLoader();
            List<PropertySource<?>> list = null;
            list = propertyLoader.load("mh", new ClassPathResource("demo.properties"));
            System.out.printf("mh.*: %s%n", list.get(0).getSource());
            // 加载yaml文件
            PropertySourceLoader yamlLoader = new YamlPropertySourceLoader();
            List<PropertySource<?>> yamls = yamlLoader.load("mh", new ClassPathResource("demo.yml"));
            System.out.printf("mh.*: %s%n", yamls.get(0).getSource());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
